package lumingweihua.future.cn.lumingweihua.ui.order;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhusx.core.adapter.Lib_BaseRecyclerAdapter;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Densitys;
import com.zhusx.core.utils._Intents;
import com.zhusx.core.utils._Lists;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.PhotoActivity;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.WuliuData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;

/**
 * Created by z1986 on 2018/3/16.
 */

public class WuLiuActivity extends BaseCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;
    @BindView(R.id.iv_image)
    FrescoImageView ivImage;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_license)
    TextView tvLicense;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    WuliuData data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_wuliu);
        ButterKnife.bind(this);
        String car_id = getIntent().getStringExtra(_EXTRA_String_ID);
        String good_num = getIntent().getStringExtra(_EXTRA_String);
        LoadData<WuliuData> loadData = new LoadData<>(LoadData.Api.货主订单运输详情, this);
        loadData._setOnLoadingListener(new LoadingHelper<WuliuData>(layoutContent, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<WuliuData> data) {
                init(data.getData());
            }
        });
        loadData._loadData(car_id, good_num);
    }

    private void init(WuliuData en) {
        this.data = en;
        tvName.setText(en.car_name);
        tvLicense.setText(en.license);
        tvWeight.setText("装货: " + en.rough_weight + "吨");
        ivImage.setImageURI(en.avatar_url);
        if (en.dongtai != null) {
            final LayoutInflater inflater = LayoutInflater.from(this);
            recyclerView.setAdapter(new Lib_BaseRecyclerAdapter<WuliuData.Bean>(en.dongtai) {
                @Override
                protected void __bindViewHolder(_ViewHolder viewHolder, int i, WuliuData.Bean s) {
                    viewHolder.setText(R.id.tv_message, s.dynamic);
                    viewHolder.setText(R.id.tv_date, s.create_time);
                    viewHolder.getView(R.id.line).setVisibility(i == 0 ? View.INVISIBLE : View.VISIBLE);
                    if (_Lists.isEmpty(s.img)) {
                        viewHolder.getView(R.id.layout_images).setVisibility(View.GONE);
                    } else {
                        LinearLayout imageLayout = viewHolder.getView(R.id.layout_images);
                        imageLayout.setVisibility(View.VISIBLE);
                        imageLayout.removeAllViews();
                        for (int j = 0; j < s.img.size(); j++) {
                            FrescoImageView iv = (FrescoImageView) inflater.inflate(R.layout.item_grid_images, null, false);
                            final String url = s.img.get(j);
                            iv.setImageURI(url);
                            iv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    startActivity(new Intent(view.getContext(), PhotoActivity.class).putExtra(_EXTRA_String,url));
                                }
                            });
                            imageLayout.addView(iv, new LinearLayout.LayoutParams(_Densitys.dip2px(WuLiuActivity.this, 70), _Densitys.dip2px(WuLiuActivity.this, 60)));
                        }
                    }
                }

                @Override
                protected int __getLayoutResource(int i) {
                    return R.layout.item_list_transport;
                }
            });
        }
    }

    @OnClick({R.id.tv_driver})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_driver:
                if (data != null && !TextUtils.isEmpty(data.car_mobile)) {
                    new AlertDialog.Builder(this)
                            .setMessage("是否拨打电话给司机?")
                            .setNegativeButton("拨打", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    _Intents.dialPhone(WuLiuActivity.this, data.car_mobile);
                                }
                            })
                            .setPositiveButton("取消", null)
                            .create()
                            .show();
                }

                break;
        }
    }
}
